

//
//  WZUserModel.swift
//  weibo
//
//  Created by mac on 16/2/26.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class WZUserModel: NSObject,NSCoding {
    /*   "access_token" = "2.00HPOVSD0g47BZ27691a73d6LPEkSB";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 3021815465;*/
    
    var access_token: String?
    var expires_in: NSTimeInterval = 0
    var remind_in: String?
    var uid: String?
    var profile_image_url: String?
    var screen_name: String?
    
    
//    MARK:重写INIT方法否则一经重载了KVC构造方法，系统默认不提供初始化方法
    override init() {
        super.init()
    }
    
    init(dict: [String:NSObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
//    MARK: 实现解档 归档的协议方法
    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(remind_in, forKey: "remind_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        
    }
    
    required   init?(coder aDecoder: NSCoder) {
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.expires_in = aDecoder.decodeObjectForKey("expires_in") as! NSTimeInterval
        self.remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }

//     MARK: 实现存储模型的方法供外部调用
    func savaUserModel(fileName: String){
        
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        let filePath = (path as NSString).stringByAppendingPathComponent(fileName)
        
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
        
    }
    //     MARK: 读取数据的方法供外部调用

   class func readModel(fileName: String)->WZUserModel?{
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        let filePath = (path as NSString).stringByAppendingPathComponent(fileName)
    print(filePath)
      return  NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? WZUserModel
    
        
        
    }
}

